﻿
namespace NoNameGoServer.Client.Commands {
    public class MatchRequestCommand : Command {
        private readonly string _opponentName;
        private readonly StoneColor _color;
        private readonly int _boardSize;
        private readonly int _time;
        private readonly int _byoyomiMinutes;

        public MatchRequestCommand(string opponentName, StoneColor color, int boardSize, int time, int byoyomiMinutes) {
            _opponentName = opponentName;
            _byoyomiMinutes = byoyomiMinutes;
            _time = time;
            _boardSize = boardSize;
            _color = color;
        }

        public override void Execute(NNGSClient nngsClient) {
            string cmd = string.Format("match {0} {1} {2} {3} {4}\n", _opponentName, _color == StoneColor.White ? "W" : "B", _boardSize, _time, _byoyomiMinutes);
            nngsClient.SendAsync(this, cmd, null);
        }

		public override void ProcessReceivedLine(NNGSClient nngsClient, string line, out bool processed) {
			if (line.StartsWith("5 That player is currently not accepting matches.")) {
				processed = true;
				throw new NNGSClientException(string.Format("'{0}' is currently not accepting matches.", _opponentName));
			}

			if (line.StartsWith("24 *SYSTEM*: You are in disconnected game.")) {
				processed = true;
				throw new NNGSClientException("Received 'You are in a disconnected game' try again after the timeout.");
			}

			base.ProcessReceivedLine(nngsClient, line, out processed);
		}
    }
}
